package fr.babostesting.stage3d.starling.virtual 
{
	import com.greensock.TweenMax;
	import flash.display.BitmapData;
	import flash.text.TextFormatAlign;
	import fr.babos.graphic.tools.BitmapCache;
	import starling.display.Image;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;
	import starling.textures.Texture;
	import starling.textures.TextureSmoothing;
	
	/**
	 * @author ZoulouX
	 */
	public class LineElement extends Sprite 
	{
		protected static var __backgroundTexture		:Texture;
		
		
		protected var _item								:Object;
		
		protected var _background						:Image;
		
		protected var _image							:Image;
		
		protected var _title							:TextField;
		
		protected var _text								:TextField;
		
		protected var _backImage						:Quad;
		
		protected var _bitmapCache						:BitmapCache;
		
		
		public var index								:int;
		
		
		public function LineElement (pData:Object, pBitmapCache:BitmapCache)
		{
			_item = pData;
			_bitmapCache = pBitmapCache;
			
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
		}
		
		protected function addedToStageHandler (event:Event):void
		{
			// Générer l'image de fond des cases
			if (__backgroundTexture == null)
			{
				var backgroundBitmapData:BitmapData = new BitmapData(2, 100, false, 0xFFFFFFFF);
				backgroundBitmapData.setPixel(0, 99, 0xCCCCCC);
				backgroundBitmapData.setPixel(1, 99, 0xCCCCCC);
				
				__backgroundTexture = Texture.fromBitmapData(backgroundBitmapData);
				__backgroundTexture.repeat = true;
			}
			
			// Le fond de la case
			_background = new Image(__backgroundTexture);
			_background.smoothing = TextureSmoothing.NONE;
			
			_background.width = stage.stageWidth;
			_background.height = 100;
			
			addChild(_background);
			
			if (_item != null)
			{
				// Le fond de l'avatar
				_backImage = new Quad(80, 80);
				
				_backImage.setVertexColor(0, 0xDDDDDD);
				_backImage.setVertexColor(1, 0xDDDDDD);
				_backImage.setVertexColor(2, 0x999999);
				_backImage.setVertexColor(3, 0x999999);
				
				_backImage.width = 80;
				_backImage.height = 80;
				
				_backImage.x = 10;
				_backImage.y = 10;
				
				addChild(_backImage);
				
				_bitmapCache.load(_item.avatar, avatarSuccessHandler);
				
				// Le titre
				_title = new TextField(stage.stageWidth - 120, 20, _item.username, "Verdana", 11, 0x666666);
				_title.hAlign = TextFormatAlign.LEFT;
				_title.vAlign = "top";
				
				_title.x = 102;
				_title.y = 12;
				
				addChild(_title);
				
				// Le contenu
				_text = new TextField(stage.stageWidth - 120, 70, _item.text);
				_text.hAlign = TextFormatAlign.LEFT;
				_text.vAlign = "top";
				
				_text.x = 102;
				_text.y = 30;
				
				addChild(_text);
			}
		}
		
		protected function avatarSuccessHandler (pBitmapData:BitmapData, pFromCache:Boolean):void
		{
			if (_backImage == null)
				return;
			
			_image = new Image(Texture.fromBitmapData(pBitmapData));
			//_image.smoothing = TextureSmoothing.NONE;
			
			_image.width = 80;
			_image.height = 80;
			
			_image.x = 10;
			_image.y = 10;
			
			addChild(_image);
			
			_image.texture = Texture.fromBitmapData(pBitmapData);
			
			TweenMax.from(_image, pFromCache ? 0 : .25, {
				alpha: 0,
				onComplete: _backImage.removeFromParent,
				onCompleteParams: [true]
			});
		}
	}
}